package com.atlas.alpha;


import android.app.*;
import android.content.*;
import android.os.*;
import android.view.*;
import android.view.View.OnClickListener;
import android.widget.*;


public class MainActivity extends Activity  {
    
    private EditText etUsername;
    private EditText etPassword;
    private Button btnLogin;
    private Button btnShowRegisterActivity;
    private TextView lblStatus;
    
    /**
     * The serialization (saved instance state) Bundle key representing the
     * current dropdown position.
     */
    private static final String STATE_SELECTED_NAVIGATION_ITEM = "selected_navigation_item";
    LoginDataBaseAdapter loginDataBaseAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        loginDataBaseAdapter = new LoginDataBaseAdapter(this);
        loginDataBaseAdapter = loginDataBaseAdapter.open();
        
        //Hook up refrences with gui elements
        etUsername = (EditText)findViewById(R.id.etDesiredUsername);
        etPassword = (EditText)findViewById(R.id.etPassword);
        btnLogin = (Button)findViewById(R.id.btnLogin);
        btnShowRegisterActivity = (Button)findViewById(R.id.btnAddUserToDB);
        lblStatus = (TextView)findViewById(R.id.lblStatus);
        
        /*listeners to clear text when user gives the field focus
         * */
        etUsername.setOnFocusChangeListener(new View.OnFocusChangeListener() {
			@Override
			public void onFocusChange(View v, boolean hasFocus) {
				// TODO Auto-generated method stub
				if(hasFocus)
					etUsername.setText("");
			}
		}); 
        etPassword.setOnFocusChangeListener(new View.OnFocusChangeListener() {
			@Override
			public void onFocusChange(View v, boolean hasFocus) {
				// TODO Auto-generated method stub
				if(hasFocus)
					etPassword.setText("");
			}
		}); 
        
        
        //Handle click for login button
        btnLogin.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
            	String username = etUsername.getText().toString();
                String password = etPassword.getText().toString();
                String storedPassword = loginDataBaseAdapter.getUserPass(username);
                if(password.equals(storedPassword)){
                	lblStatus.setText("Login Successful");
                    //switch activity to selection screen
                    Intent iSeletion = new Intent(v.getContext(), SelectionScreen.class);
                    startActivityForResult(iSeletion, 0);
                } else {
                	lblStatus.setText("Login Failed. Wrong Username/Password");
                }
            	
            	
            }
        });
        //Handle click for register button
        btnShowRegisterActivity.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
            	Intent iRegister = new Intent(v.getContext(), RegisterActivity.class);
            	startActivityForResult(iRegister, 0);
            }
        });
        
/*        // Set up the action bar to show a dropdown list.
        final ActionBar actionBar = getActionBar();
        actionBar.setDisplayShowTitleEnabled(false);
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);

        // Set up the dropdown list navigation in the action bar.
        actionBar.setListNavigationCallbacks(
                // Specify a SpinnerAdapter to populate the dropdown list.
                new ArrayAdapter<String>(
                        actionBar.getThemedContext(),
                        android.R.layout.simple_list_item_1,
                        android.R.id.text1,
                        new String[] {
                                getString(R.string.title_section1),
                                getString(R.string.title_section2),
                                getString(R.string.title_section3),
                        }),
                this);*/
    }


    @Override
    public void onRestoreInstanceState(Bundle savedInstanceState) {
        // Restore the previously serialized current dropdown position.
        if (savedInstanceState.containsKey(STATE_SELECTED_NAVIGATION_ITEM)) {
            getActionBar().setSelectedNavigationItem(
                    savedInstanceState.getInt(STATE_SELECTED_NAVIGATION_ITEM));
        }
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        // Serialize the current dropdown position.
        outState.putInt(STATE_SELECTED_NAVIGATION_ITEM,
                getActionBar().getSelectedNavigationIndex());
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    
    
/*    @Override
    public boolean onNavigationItemSelected(int position, long id) {
        // When the given dropdown item is selected, show its contents in the
        // container view.
        Fragment fragment = new DummySectionFragment();
        Bundle args = new Bundle();
        args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position + 1);
        fragment.setArguments(args);
        getSupportFragmentManager().beginTransaction()
                .replace(R.id.container, fragment)
                .commit();
        return true;
    }

    *//**
     * A dummy fragment representing a section of the app, but that simply
     * displays dummy text.
     *//*
    public static class DummySectionFragment extends Fragment {
        *//**
         * The fragment argument representing the section number for this
         * fragment.
         *//*
        public static final String ARG_SECTION_NUMBER = "section_number";

        public DummySectionFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main_dummy, container, false);
            TextView dummyTextView = (TextView) rootView.findViewById(R.id.section_label);
            dummyTextView.setText(Integer.toString(getArguments().getInt(ARG_SECTION_NUMBER)));
            return rootView;
        }
    }*/
    @Override
    protected void onDestroy(){
    	super.onDestroy();
    	loginDataBaseAdapter.close();
    }

}
